{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Valid Word Square"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: validWordSquare"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #有效的单词方块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个单词序列，判断其是否形成了一个有效的单词方块。</p>\n",
    "\n",
    "<p>有效的单词方块是指此由单词序列组成的文字方块的&nbsp;第 k 行 和&nbsp;第 k 列 (0 &le; <em>k</em> &lt; max(行数, 列数)) 所显示的字符串完全相同。</p>\n",
    "\n",
    "<p><strong>注意：</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li>给定的单词数大于等于 1 且不超过 500。</li>\n",
    "\t<li>单词长度大于等于 1 且不超过 500。</li>\n",
    "\t<li>每个单词只包含小写英文字母&nbsp;<code>a-z</code>。</li>\n",
    "</ol>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>\n",
    "[\n",
    "  &quot;abcd&quot;,\n",
    "  &quot;bnrt&quot;,\n",
    "  &quot;crmy&quot;,\n",
    "  &quot;dtye&quot;\n",
    "]\n",
    "\n",
    "<strong>输出：</strong>\n",
    "true\n",
    "\n",
    "<strong>解释：</strong>\n",
    "第 1 行和第 1 列都是 &quot;abcd&quot;。\n",
    "第 2 行和第 2 列都是 &quot;bnrt&quot;。\n",
    "第 3 行和第 3 列都是 &quot;crmy&quot;。\n",
    "第 4 行和第 4 列都是 &quot;dtye&quot;。\n",
    "\n",
    "因此，这是一个有效的单词方块。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>\n",
    "[\n",
    "  &quot;abcd&quot;,\n",
    "  &quot;bnrt&quot;,\n",
    "  &quot;crm&quot;,\n",
    "  &quot;dt&quot;\n",
    "]\n",
    "\n",
    "<strong>输出：</strong>\n",
    "true\n",
    "\n",
    "<strong>解释：</strong>\n",
    "第 1 行和第 1 列都是 &quot;abcd&quot;。\n",
    "第 2 行和第 2 列都是 &quot;bnrt&quot;。\n",
    "第 3 行和第 3 列都是 &quot;crm&quot;。\n",
    "第 4 行和第 4 列都是 &quot;dt&quot;。\n",
    "\n",
    "因此，这是一个有效的单词方块。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>\n",
    "[\n",
    "  &quot;ball&quot;,\n",
    "  &quot;area&quot;,\n",
    "  &quot;read&quot;,\n",
    "  &quot;lady&quot;\n",
    "]\n",
    "\n",
    "<strong>输出：</strong>\n",
    "false\n",
    "\n",
    "<strong>解释：</strong>\n",
    "第 3 行是 &quot;read&quot; ，然而第 3 列是 &quot;lead&quot;。\n",
    "\n",
    "因此，这 <strong>不是</strong> 一个有效的单词方块。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [valid-word-square](https://leetcode.cn/problems/valid-word-square/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [valid-word-square](https://leetcode.cn/problems/valid-word-square/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"abcd\",\"bnrt\",\"crmy\",\"dtye\"]', '[\"abcd\",\"bnrt\",\"crm\",\"dt\"]', '[\"ball\",\"area\",\"read\",\"lady\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def validWordSquare(self, words: list[str]) -> bool:\n",
    "    #首先确认该序列是不是方阵\n",
    "    row_num=len(words)\n",
    "    col_num=len(words[0])\n",
    "    if(row_num!=col_num):\n",
    "      return False\n",
    "    #如果是方阵，再判断其是否对称\n",
    "    else:\n",
    "      i=0\n",
    "      while(i<row_num):\n",
    "        j=0\n",
    "        while(j<len(words[i])):\n",
    "          #判断i行j列的对称元是否存在\n",
    "          i_r=j\n",
    "          j_r=i\n",
    "          if(i_r <len(words)):\n",
    "            if(j_r <len(words[i_r])):\n",
    "              #若存在且两元相等则下一个\n",
    "              if(words[i][j]==words[i_r][j_r]):\n",
    "                j+=1\n",
    "              else:\n",
    "                return False\n",
    "            else:\n",
    "              return False\n",
    "          else:\n",
    "            return False\n",
    "        i+=1\n",
    "      return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        n = len(words)\n",
    "        # m = len(words[0])\n",
    "\n",
    "        for i in range(n):\n",
    "            s = ''\n",
    "            for j in range(n):\n",
    "                if i < len(words[j]):\n",
    "                    s += words[j][i]\n",
    "            if s != words[i]:\n",
    "                return False\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        #这个比较好理解\n",
    "        try:\n",
    "            length = len(words)\n",
    "            # 第一个单词的长度必然与单词序列的长度相等\n",
    "            if len(words[0]) != length:\n",
    "                return False\n",
    "            for i in range(length):\n",
    "                for j in range(len(words[i])):\n",
    "                    if words[i][j] != words[j][i]:\n",
    "                        return False\n",
    "            return True\n",
    "        except:\n",
    "            return False\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def validWordSquare(self, words: list[str]) -> bool:\n",
    "    #首先确认该序列是不是方阵\n",
    "    row_num=len(words)\n",
    "    col_num=len(words[0])\n",
    "    if(row_num!=col_num):\n",
    "      return False\n",
    "    #如果是方阵，再判断其是否对称\n",
    "    else:\n",
    "      for i in range(row_num):\n",
    "        for j in range(len(words[i])):\n",
    "          #判断i行j列的对称元是否存在\n",
    "          i_r=j\n",
    "          j_r=i\n",
    "          if(i_r <len(words)):\n",
    "            if(j_r <len(words[i_r])):\n",
    "              #若存在且两元相等则下一个\n",
    "              if(words[i][j]!=words[i_r][j_r]):\n",
    "                return False\n",
    "            else:\n",
    "              return False\n",
    "          else:\n",
    "            return False\n",
    "      return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        # 关于主对角线对称\n",
    "        Row = len(words)\n",
    "        for r in range(Row):\n",
    "            for c in range(len(words[r])):\n",
    "                if c >= Row:        #为了让第3个if不要越界\n",
    "                    return False\n",
    "                if r >= len(words[c]):  #为了让第3个if不要越界\n",
    "                    return False\n",
    "                if words[r][c] != words[c][r]:  #关于diagonal对称\n",
    "                    return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        n = len(words)\n",
    "        for i in range(n):\n",
    "            for j in range(len(words[i])):\n",
    "                try:\n",
    "                    if words[i][j]!=words[j][i]: return False\n",
    "                except:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def validWordSquare(self, words: list[str]) -> bool:\n",
    "    #首先确认该序列是不是方阵\n",
    "    row_num=len(words)\n",
    "    col_num=len(words[0])\n",
    "    if(row_num!=col_num):\n",
    "      return False\n",
    "    #如果是方阵，再判断其是否对称\n",
    "    else:\n",
    "      for i in range(row_num):\n",
    "        for j in range(len(words[i])):\n",
    "          #判断i行j列的对称元是否存在\n",
    "          i_r=j\n",
    "          j_r=i\n",
    "          if(i_r <len(words)):\n",
    "            if(j_r <len(words[i_r])):\n",
    "              #若存在且两元相等则下一个\n",
    "              if(words[i][j]!=words[i_r][j_r]):\n",
    "                return False\n",
    "            else:\n",
    "              return False\n",
    "          else:\n",
    "            return False\n",
    "      return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        n = len(words)\n",
    "        for i, hang in enumerate(words):\n",
    "            for j, v in enumerate(hang):\n",
    "                try:\n",
    "                    if words[j][i] != v:\n",
    "                        return False\n",
    "                except:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def validWordSquare(self, words: list[str]) -> bool:\n",
    "    #首先确认该序列是不是方阵\n",
    "    row_num=len(words)\n",
    "    col_num=len(words[0])\n",
    "    if(row_num!=col_num):\n",
    "      return False\n",
    "    #如果是方阵，再判断其是否对称\n",
    "    else:\n",
    "      i=0\n",
    "      while(i<row_num):\n",
    "        j=0\n",
    "        while(j<len(words[i])):\n",
    "          #判断i行j列的对称元是否存在\n",
    "          i_r=j\n",
    "          j_r=i\n",
    "          if(i_r <len(words)):\n",
    "            if(j_r <len(words[i_r])):\n",
    "              #若存在且两元相等则下一个\n",
    "              if(words[i][j]==words[i_r][j_r]):\n",
    "                j+=1\n",
    "              else:\n",
    "                return False\n",
    "            else:\n",
    "              return False\n",
    "          else:\n",
    "            return False\n",
    "        i+=1\n",
    "      return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        for i in range(len(words)):\n",
    "            col_length = len(words[i])\n",
    "            for j in range(col_length):\n",
    "                if len(words) < j+1 or len(words[j]) < i+1:\n",
    "                    return False\n",
    "                else:\n",
    "                    if words[i][j] != words[j][i]:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        Row = len(words)\n",
    "        for r in range(Row):\n",
    "            for c in range(len(words[r])):\n",
    "                if c >= Row:        #为了让第3个if不要越界\n",
    "                    return False\n",
    "                if r >= len(words[c]):  #为了让第3个if不要越界\n",
    "                    return False\n",
    "                if words[r][c] != words[c][r]:  #关于diagonal对称\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        for i in range(len(words)):\n",
    "            for j in range(len(words[i])):\n",
    "                if j>=len(words): \n",
    "                    return False\n",
    "                elif i>=len(words[j]):\n",
    "                    return False\n",
    "                elif words[i][j]!=words[j][i]:\n",
    "                    return False\n",
    "        return True\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#按列获得字母再对比，这样效率更高\n",
    "class Solution:\n",
    "  def validWordSquare(self, words: list[str]) -> bool:\n",
    "    #首先确认该序列是不是方阵\n",
    "    row_num=len(words)\n",
    "    col_num=len(words[0])\n",
    "    for j in range(col_num):\n",
    "      col_word=\"\"\n",
    "      for i in range(row_num):\n",
    "        if(j<len(words[i])):\n",
    "          col_word+=words[i][j]\n",
    "        #注意，这里只是按列扒字母，只要把各列的每一行记录就行，不需要判断对角元存在，因此不要else return False\n",
    "        #else:\n",
    "        #  return False\n",
    "      if(col_word!=words[j]):\n",
    "        return False\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        m = len(words)\n",
    "        for i, w in enumerate(words):\n",
    "            for j, c in enumerate(w):\n",
    "                if j >= m or i >= len(words[j]) or c != words[j][i]:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#按列获得字母再对比，这样效率更高\n",
    "class Solution:\n",
    "  def validWordSquare(self, words: list[str]) -> bool:\n",
    "    #首先确认该序列是不是方阵\n",
    "    row_num=len(words)\n",
    "    col_num=len(words[0])\n",
    "    for j in range(col_num):\n",
    "      col_word=\"\"\n",
    "      for i in range(row_num):\n",
    "        if(j<len(words[i])):\n",
    "          col_word+=words[i][j]\n",
    "        #注意，这里只是按列扒字母，只要把各列的每一行记录就行，不需要判断对角元存在，因此不要else return False\n",
    "        #else:\n",
    "        #  return False\n",
    "        # 若j超限，直接break该层\n",
    "        else:\n",
    "          break\n",
    "      if(col_word!=words[j]):\n",
    "        return False\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def validWordSquare(self, words: list[str]) -> bool:\n",
    "    #首先确认该序列是不是方阵\n",
    "    row_num=len(words)\n",
    "    col_num=len(words[0])\n",
    "    for j in range(col_num):\n",
    "      col_word=\"\"\n",
    "      for i in range(row_num):\n",
    "        if(j<len(words[i])):\n",
    "          col_word+=words[i][j]\n",
    "      if(col_word!=words[j]):\n",
    "        return False\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "\n",
    "        n = len(words)\n",
    "\n",
    "        for i in range(n):\n",
    "            m =len(words[i])\n",
    "            if m > n:\n",
    "                return False\n",
    "            elif m < n:\n",
    "                words[i] += \"*\"*(n - m)\n",
    "\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if words[i][j] != words[j][i]:\n",
    "                    return False\n",
    "\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        n = len(words)\n",
    "        m = max(map(lambda x:len(x), words))\n",
    "        s = min(m, n)\n",
    "        for i in range(s):\n",
    "            word = words[i]\n",
    "            same = True\n",
    "            for j in range(max(len(word), n)):\n",
    "                a = words[i][j] if j<len(word) else ''\n",
    "                b = words[j][i] if j<len(words) and i<len(words[j]) else ''\n",
    "                if a != b:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        x=len(words)\n",
    "        y=len(words[0])\n",
    "        if x!=y:\n",
    "            return False\n",
    "        for i in range(x):\n",
    "            st=\"\"\n",
    "            for k in range(x):\n",
    "                # 加纵列\n",
    "                if i < len(words[k]):\n",
    "                    st+=words[k][i]\n",
    "                else:\n",
    "                    st+=\"\"\n",
    "            if st!=words[i]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        n = len(words)\n",
    "        for i in range(n):\n",
    "            for j in range(len(words[i])):\n",
    "                try:\n",
    "                    if words[i][j] != words[j][i]:\n",
    "                        return False\n",
    "                except:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        num_row, num_col = len(words), max(len(word) for word in words)\n",
    "        if num_row != num_col:\n",
    "            return False\n",
    "\n",
    "        for i, word in enumerate(words):\n",
    "            if len(word) < num_row:\n",
    "                words[i] += (num_row - len(word)) * ' '\n",
    "\n",
    "        cur_start_index = 1\n",
    "        for index, row in enumerate(words):\n",
    "            for compare_index in range(cur_start_index, len(row)):\n",
    "                if words[index][compare_index] != words[compare_index][index]:\n",
    "                    return False\n",
    "            cur_start_index += 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        m = len(words)\n",
    "        n = len(words[0])\n",
    "        for j in range(n):\n",
    "            col = ''\n",
    "            for i in range(m):\n",
    "                if j < len(words[i]):\n",
    "                    col += words[i][j]\n",
    "            if col != words[j]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        a=True\n",
    "        for i in range(len(words)):\n",
    "            for j in range(len(words[i])):\n",
    "                if j+1 > len(words):\n",
    "                    a=False\n",
    "                elif i+1 > len(words[j]):\n",
    "                    a=False \n",
    "                elif words[i][j] != words[j][i]:\n",
    "                    a=False\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        m = len(words)\n",
    "        for i in range(m):\n",
    "            n = len(words[i])\n",
    "            if n > m:\n",
    "                return False\n",
    "            for j in range(n):\n",
    "                if len(words[j]) - 1 < i or words[i][j] != words[j][i]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        \n",
    "        for i in range(len(words)):\n",
    "            col = [s[i] for s in words if i < len(s)]\n",
    "            col_word = ''.join(col)\n",
    "            if col_word != words[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        rows = len(words)\n",
    "        cols = max([len(x) for x in words])\n",
    "        for k in range(max(rows, cols)):\n",
    "            s = ''.join([x[k:k+1] for x in words])\n",
    "            if len(s) > 0 and (k >= rows or words[k] != s):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        max_word_len = max([len(x) for x in words])\n",
    "        if len(words) != max_word_len:\n",
    "            return False\n",
    "        for i in range(len(words)):\n",
    "            if words[i] != len(words):\n",
    "                words[i] = words[i] + (len(words) - len(words[i])) * \" \"\n",
    "        for i in range(len(words)):\n",
    "            for j in range(i):\n",
    "                if words[i][j] != words[j][i]:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        m= len(words)\n",
    "        for i in range(m):\n",
    "            k_row = words[i]\n",
    "            k_col = ''\n",
    "            for j in range(m):\n",
    "                if i < len(words[j]):\n",
    "                    k_col += words[j][i]\n",
    "            if k_col != k_row:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        \"\"\"\n",
    "        j=0\n",
    "        i=0\n",
    "        col=\"\"\n",
    "        while i < len(words):\n",
    "            #print(\"i\",i)\n",
    "            row = words[i]\n",
    "            while j < len(words) and len(words[j]) > i:\n",
    "                #if  words[i][j]!=words[j][i]:\n",
    "                #    return False\n",
    "                #else :\n",
    "                #    print(\"bigsmall\")\n",
    "                #    return False\n",
    "                len(words)\n",
    "                col += words[j][i]\n",
    "                j=j+1\n",
    "            if row != col:\n",
    "                return False\n",
    "            i=i+1\n",
    "\n",
    "        if i==len(words):\n",
    "            return True\n",
    "        \"\"\"\n",
    "        n = len(words)\n",
    "        for i in range(n):\n",
    "            row = words[i]\n",
    "            col = \"\"\n",
    "            j = 0\n",
    "            while j < n and len(words[j]) > i:\n",
    "                col += words[j][i]\n",
    "                j += 1\n",
    "            if row != col:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        # def if_true(words):\n",
    "            \n",
    "        #     col = words[0]\n",
    "        #     row = ''.join([wi[0] for wi in words])\n",
    "\n",
    "        #     return col==row\n",
    "        \n",
    "        # k = len(words)\n",
    "        # if k==1:\n",
    "        #     return True\n",
    "        # for i in range(k):\n",
    "        #     if k-len(words[i]) < 0:\n",
    "        #         return False\n",
    "        #     words[i] = words[i]+(k-len(words[i]))*' '\n",
    "        \n",
    "        # return if_true(words) and self.validWordSquare([wi[1:] for wi in words[1:]])\n",
    "\n",
    "        try:\n",
    "            length = len(words)\n",
    "            if len(words[0]) != length:\n",
    "                return False\n",
    "            for i in range(length):\n",
    "                for j in range(len(words[i])):\n",
    "                    if words[i][j] != words[j][i]:\n",
    "                        return False\n",
    "            return True\n",
    "        except:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        for i in range(len(words)):\n",
    "            for j in range(len(words[i])):\n",
    "                if len(words)<=j or len(words[j])<=i or words[j][i] != words[i][j]:\n",
    "                    return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:       \n",
    "        for i in range(len(words)):\n",
    "            for j in range(len(words[i])):\n",
    "                if j >= len(words) or i >= len(words[j]) or words[i][j] != words[j][i]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        for i in range(len(words)):\n",
    "            for j in range(len(words[i])):\n",
    "                try:\n",
    "                    if words[i][j] != words[j][i]:\n",
    "                        return False\n",
    "                except IndexError:\n",
    "                    return False\n",
    "                \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        for i in range(len(words[0])):\n",
    "            col = ''\n",
    "    \n",
    "            for j in range(len(words)):\n",
    "                if i < len(words[j]):\n",
    "                    col += words[j][i]\n",
    "            if col != words[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        col = len(words[0])\n",
    "        row = len(words)\n",
    "        for i in range(col):\n",
    "            s = ''\n",
    "            for j in range(row):\n",
    "                if i < len(words[j]):\n",
    "                    s += words[j][i]\n",
    "            if s != words[i]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        m = len(words)\n",
    "        n = len(words[0])\n",
    "        for j in range(n):\n",
    "            col = ''\n",
    "            for i in range(m):\n",
    "                if j < len(words[i]):\n",
    "                    col += words[i][j]\n",
    "            if col != words[j]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        n = len(words)\n",
    "        for i in range(n):\n",
    "            for j in range(len(words[i])): \n",
    "                if j >=n or i >= len(words[j]):\n",
    "                    return False\n",
    "                elif words[i][j] != words[j][i]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        for i in range(len(words)):\n",
    "            string=''\n",
    "            for j in range(len(words)):\n",
    "                if i<len(words[j]) and j<len(words[i]):\n",
    "                    string+=words[j][i]\n",
    "            print(string)\n",
    "            if string!=words[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        if len(words[0])!=len(words):\n",
    "            return False\n",
    "        for i in range(len(words)):\n",
    "            string=''\n",
    "            for j in range(len(words)):\n",
    "                if i<len(words[j]) and j<len(words[i]):\n",
    "                    string+=words[j][i]\n",
    "            print(string)\n",
    "            if string!=words[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        maxlen=max([len(word) for word in words])\n",
    "        words=[word +'*'*(maxlen-len(word)) for word in words]\n",
    "        w=zip(*words)\n",
    "        w=[''.join(z) for z in w]\n",
    "        return True if w==words else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        row=len(words)\n",
    "        for r in range(row):\n",
    "            for c in range(len(words[r])):\n",
    "                if c>=row:\n",
    "                    return False\n",
    "                if r>=len(words[c]):\n",
    "                    return False\n",
    "                if words[r][c]!=words[c][r]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        max_len = max(len(word) for word in words)\n",
    "        words = [word.ljust(max_len) for word in words]\n",
    "        tempt = zip(*words)\n",
    "        words_ = [\"\".join(item) for item in tempt]\n",
    "        return words_ == words\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        collen = len(words[0])\n",
    "        rowlen = len(words)\n",
    "        for j in range(collen):\n",
    "            col = ''\n",
    "            for i in range(rowlen):\n",
    "                if j < len(words[i]):\n",
    "                    col += words[i][j]\n",
    "            if col != words[j]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        m, n = len(words), len(words[0])\n",
    "        col = []\n",
    "        for i in range(n):\n",
    "            data = \"\"\n",
    "            for j in range(m):\n",
    "                try:\n",
    "                    data += words[j][i]\n",
    "                except:\n",
    "                    data += \"\"\n",
    "            col.append(data)\n",
    "        return words == col\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        max_len = max([len(word) for word in words])\n",
    "        words = [word + (max_len - len(word)) * \"#\" for word in words]\n",
    "        words_ = zip(*words)\n",
    "        words_ = [\"\".join(item) for item in words_]\n",
    "        return words_ == words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        col = []\n",
    "        for i in range(len(words[0])):\n",
    "            data = ''\n",
    "            for j in range(len(words)):\n",
    "                try:\n",
    "                    data += words[j][i]\n",
    "                except:\n",
    "                    data += ''\n",
    "            col.append(data)\n",
    "        return words == col\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        max_len = max(len(word) for word in words)\n",
    "        words = [word.ljust(max_len) for word in words]\n",
    "        tempt = zip(*words)\n",
    "        words_ = [\"\".join(item) for item in tempt]\n",
    "        return words_ == words\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "         max_len = max(len(word) for word in words)\n",
    "         words = [word + (max_len - len(word) )* \"#\" for word in words]\n",
    "         tempt = zip(*words)\n",
    "         words_ = [\"\".join(item) for item in tempt]\n",
    "         return words_ == words\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        record = [len(word) for word in words]\n",
    "        # print(record)\n",
    "        max_len = max(record)\n",
    "        for i, word in enumerate(words):\n",
    "            words[i] = word + '#'*(max_len - len(word))\n",
    "        word_T = map(''.join, zip(*words))\n",
    "        return words == [word for word in word_T]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        col_words = []\n",
    "        for j in range(len(words[0])):\n",
    "            col_word = \"\"\n",
    "            for i in range(len(words)):\n",
    "                try:\n",
    "                    col_word += words[i][j]\n",
    "                except:\n",
    "                    col_word += \"\"\n",
    "            col_words.append(col_word)\n",
    "        return words == col_words\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        col = []\n",
    "        for i in range(len(words[0])):\n",
    "            data = \"\"\n",
    "            for j in range(len(words)):\n",
    "                try:\n",
    "                    data += words[j][i]\n",
    "                except:\n",
    "                    data +=\"\"\n",
    "            col.append(data)\n",
    "        return words == col\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        mx = max(len(word) for word in words)\n",
    "        words = [word + (mx - len(word)) * '.' for word in words]\n",
    "        tmp = zip(*words)\n",
    "        word_col = [''.join(x) for x in tmp]\n",
    "        return words == word_col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        n = len(words)\n",
    "        col = [''] * n\n",
    "        for i in range(n):\n",
    "            for j in range(len(words[i])):\n",
    "                if j >= n or i >= len(words[j]):\n",
    "                    continue\n",
    "                col[i] += words[j][i]\n",
    "        return words == col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        m = len(max(words,key = len))\n",
    "        words = [x + ' '*(m - len(x)) for x in words]\n",
    "        words1 = list(''.join(x) for x in zip(*words))\n",
    "        return words == words1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        max_len = max(len(word) for word in words)\n",
    "        words = [word + (max_len - len(word))* \"#\" for word in words]# 补全\n",
    "        tempt = zip(*words)# 创建一个迭代器（按列创建元组）\n",
    "        words_ = [\"\".join(item) for item in tempt]#重组单词块*（按列重组的）\n",
    "        return words_ == words\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        m = len(max(words,key = len))\n",
    "        words = [x + ' '*(m - len(x)) for x in words]\n",
    "        words1 = list(''.join(x) for x in zip(*words))\n",
    "        for k in range(m):\n",
    "            if words[k] != words1[k]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        col = []\n",
    "        for i in range(len(words[0])):\n",
    "            data = \"\"\n",
    "            for j in range(len(words)):\n",
    "                if i < len(words[j]):  # 确保不会越界\n",
    "                    data += words[j][i]\n",
    "            col.append(data)\n",
    "        return words == col\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        zipped_words = []\n",
    "        max_col = len(max(words, key=lambda x:len(x)))\n",
    "        i = 0\n",
    "        while i < max_col:\n",
    "            cur_word = \"\"\n",
    "            for word in words:\n",
    "                if i < len(word):\n",
    "                    cur_word += word[i]\n",
    "            zipped_words.append(cur_word)\n",
    "            i += 1\n",
    "\n",
    "        row = len(words)\n",
    "        col = len(zipped_words)\n",
    "\n",
    "        i, j = 0, 0\n",
    "        while i < len(words) and j < len(zipped_words):\n",
    "            if words[i] == zipped_words[j]:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                return False        \n",
    "        \n",
    "        return True if i == len(words) and j == len(zipped_words) else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        cols = []\n",
    "        m, n = len(words), len(words[0])\n",
    "        for c in range(n):\n",
    "            t = []\n",
    "            for r in range(m):\n",
    "                if c >= len(words[r]):\n",
    "                    break\n",
    "                t.append(words[r][c])\n",
    "            cols.append(''.join(t))\n",
    "        for x, y in zip(cols, words):\n",
    "            if x != y:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        col=[]\n",
    "        for i in range(len(words[0])):\n",
    "            data=''\n",
    "            for j in range(len(words)):\n",
    "                try:\n",
    "                    data+=words[j][i]\n",
    "                except:\n",
    "                    data+=''\n",
    "            col.append(data)\n",
    "        return words == col\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\r\n",
    "        max_len = max(len(word) for word in words)\r\n",
    "        words = [word + (max_len - len(word))* \"#\" for word in words]\r\n",
    "        tempt = zip(*words)\r\n",
    "        words_ = [\"\".join(item) for item in tempt]\r\n",
    "        return words_ == words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self,words:list[str])->bool:\n",
    "        col=[]\n",
    "        for i in range(len(words[0])):\n",
    "            data=\"\"\n",
    "            for j in range(len(words)):\n",
    "                try:\n",
    "                    data+=words[j][i]\n",
    "                except:\n",
    "                    data+=\"\"\n",
    "            col.append(data)\n",
    "        return words==col\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        col_words = []\n",
    "        n = len(words)\n",
    "        m = len(words[0])\n",
    "        for j in range(m):\n",
    "            tempstr = ''\n",
    "            for i in range(n):\n",
    "                try:\n",
    "                    tempstr += words[i][j]\n",
    "                except IndexError:\n",
    "                    print(i, j)\n",
    "            col_words.append(tempstr)\n",
    "        if n != len(col_words):\n",
    "            return False\n",
    "        for i in range(n):\n",
    "            if words[i] != col_words[i]:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        l=0\n",
    "        for i in words:\n",
    "            l=max(l,len(i))\n",
    "        dp=['']*l\n",
    "        if(len(words)!=l):\n",
    "            return False\n",
    "        for i in range(len(words)):\n",
    "            for j in range(len(words[i])):\n",
    "                dp[j]=dp[j]+words[i][j]\n",
    "        \n",
    "        for i in range(len(words)):\n",
    "            if words[i]==dp[i]:\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        tmp = words\n",
    "        p = 0\n",
    "        while tmp:\n",
    "            s = ''.join([item[0] for item in tmp])\n",
    "            tmp = [item[1:] for item in tmp if len(item[1:])>0]\n",
    "            if s != words[p]:\n",
    "                return False\n",
    "            else:\n",
    "                p += 1\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        R, C = len(words), max(len(w) for w in words)\n",
    "        if R != C:\n",
    "            return False\n",
    "        m = [[''] * C for r in range(R)]\n",
    "        for i, w in enumerate(words):\n",
    "            for j, c in enumerate(w):\n",
    "                m[i][j] = c \n",
    "        for i in range(R):\n",
    "            for j in range(i + 1, C):\n",
    "                if m[i][j] != m[j][i]:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "\n",
    "        m = len(words)\n",
    "        \n",
    "        memory = [['' for i in range(m)] for j in range(m)]\n",
    "\n",
    "        for i in range(m):\n",
    "            if len(words[i]) > m:\n",
    "                return False\n",
    "            for j in range(len(words[i])):\n",
    "                memory[i][j] = words[i][j]\n",
    "       \n",
    "        for i in range(m):\n",
    "            for j in range(i, m):\n",
    "                if memory[i][j] != memory[j][i]:\n",
    "                    return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        R, C = len(words), max(len(w) for w in words)\n",
    "        print(R, C)\n",
    "        if R != C:\n",
    "            return False\n",
    "        m = [[''] * C for r in range(R)]\n",
    "        for i, w in enumerate(words):\n",
    "            for j, c in enumerate(w):\n",
    "                m[i][j] = c \n",
    "        for i in range(R):\n",
    "            for j in range(i + 1, C):\n",
    "                if m[i][j] != m[j][i]:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        # 翻转一下,将words补齐成正方形\n",
    "        maxj = max(len(i) for i in words)\n",
    "        maxi = len(words)\n",
    "        if maxi != maxj:\n",
    "            return False\n",
    "        # 补齐正方形\n",
    "        for i in range(maxi):\n",
    "            if len(words[i]) < maxj:\n",
    "                words[i] += ' ' * (maxj-len(words[i]))\n",
    "        word2 = list(zip(*words))\n",
    "        for i in range(len(words)):\n",
    "            if ''.join(word2[i]) != words[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        max_l = max(len(word) for word in words)\n",
    "\n",
    "        res = []\n",
    "\n",
    "        for word in words:\n",
    "            res += [word + '*' * (max_l - len(word))]\n",
    "        \n",
    "        # print(res)\n",
    "        # print(list(zip(*res)))\n",
    "        \n",
    "        return res == [''.join(word) for word in list(zip(*res))]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        # 首先获取每一列的单词，按列排列的字符\n",
    "        col_words = defaultdict(list)\n",
    "        for word in words:\n",
    "            for i in range(len(word)):\n",
    "                col_words[i].append(word[i])\n",
    "        if len(col_words) != len(words):\n",
    "            return False\n",
    "        for i in range(len(words)):\n",
    "            if words[i] != ''.join(col_words[i]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        lis2,cnt=[],0\n",
    "        for i in words:\n",
    "            lis=[]\n",
    "            for j in i:\n",
    "                lis.append(j)\n",
    "            lis2.append(lis)\n",
    "        lis3 = list(zip_longest(*lis2, fillvalue=''))\n",
    "        lis3 = [[cell for cell in row if cell.strip()] for row in lis3]\n",
    "        for i in range(len(lis2)):\n",
    "            if lis2[i]==lis3[i]:\n",
    "                cnt+=1\n",
    "            else:\n",
    "                cnt=0\n",
    "                break\n",
    "        if cnt:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        lis2,cnt=[],0\n",
    "        for i in words:\n",
    "            lis=[]\n",
    "            for j in i:\n",
    "                lis.append(j)\n",
    "            lis2.append(lis)\n",
    "        lis3 = list(zip_longest(*lis2, fillvalue=''))\n",
    "        lis3 = [[cell for cell in row if cell.strip()] for row in lis3]\n",
    "        for i in range(len(lis2)):\n",
    "            if lis2[i]==lis3[i]:\n",
    "                cnt+=1\n",
    "            else:\n",
    "                cnt=0\n",
    "                break\n",
    "        if cnt:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        lis2,cnt=[],0\n",
    "        for i in words:\n",
    "            lis=[]\n",
    "            for j in i:\n",
    "                lis.append(j)\n",
    "            lis2.append(lis)\n",
    "        lis3 = list(zip_longest(*lis2, fillvalue=''))\n",
    "        lis3 = [[cell for cell in row if cell.strip()] for row in lis3]\n",
    "        for i in range(len(lis2)):\n",
    "            if lis2[i]==lis3[i]:\n",
    "                cnt+=1\n",
    "            else:\n",
    "                cnt=0\n",
    "                break\n",
    "        if cnt:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        lis2,cnt=[],0\n",
    "        for i in words:\n",
    "            lis=[]\n",
    "            for j in i:\n",
    "                lis.append(j)\n",
    "            lis2.append(lis)\n",
    "        lis3 = list(zip_longest(*lis2, fillvalue=''))\n",
    "    \n",
    "    # 移除转置后列表中的空格\n",
    "        lis3 = [[cell for cell in row if cell.strip()] for row in lis3]\n",
    "        print(lis2,lis3)\n",
    "        print(len(lis2),len(lis3))\n",
    "        for i in range(len(lis2)):\n",
    "            if lis2[i]==lis3[i]:\n",
    "                cnt+=1\n",
    "            else:\n",
    "                cnt=0\n",
    "                break\n",
    "        if cnt:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "# ball\n",
    "# asee\n",
    "# lett\n",
    "# le\n",
    "\n",
    "# abcd\n",
    "# bnrt\n",
    "# crm\n",
    "# dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validWordSquare(self, words: List[str]) -> bool:\n",
    "        lis2,cnt=[],0\n",
    "        for i in words:\n",
    "            lis=[]\n",
    "            for j in i:\n",
    "                lis.append(j)\n",
    "            lis2.append(lis)\n",
    "        lis3 = list(zip_longest(*lis2, fillvalue=''))\n",
    "    \n",
    "    # 移除转置后列表中的空格\n",
    "        lis3 = [[cell for cell in row if cell.strip()] for row in lis3]\n",
    "        print(lis2,lis3)\n",
    "        print(len(lis2),len(lis3))\n",
    "        for i in range(len(lis2)):\n",
    "            if lis2[i]==lis3[i]:\n",
    "                cnt+=1\n",
    "            else:\n",
    "                cnt=0\n",
    "                break\n",
    "        if cnt:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "# ball\n",
    "# asee\n",
    "# lett\n",
    "# le\n",
    "\n",
    "# abcd\n",
    "# bnrt\n",
    "# crm\n",
    "# dt"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
